
<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>博文管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input
          size="small"
          v-model="formInline.blog_id"
          placeholder="输入用户id"
        ></el-input>
      </el-form-item>
      <el-select v-model="selectf" placeholder="请选择分类">
        <el-option
          v-for="item in listDatas"
          :key="item.sotr_id"
          :label="item.sotr_id"
          :value="item.sort_name"
        >
        </el-option>
      </el-select>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="search"
          >搜索</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="tianjia"
          >添加</el-button
        >
      </el-form-item>
    </el-form>
    <!--列表-->

    <el-table
      size="small"
      :data="listData"
      highlight-current-row
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="blog_id" label="博文id" width="150">
      </el-table-column>
      <el-table-column sortable prop="user_id" label="用户id" width="150">
      </el-table-column>
      <el-table-column sortable prop="blog_title" label="文章标题" width="150">
      </el-table-column>

      <el-table-column sortable prop="blog_views" label="查看次数" width="100">
      </el-table-column>
      <el-table-column
        sortable
        prop="blog_comment_count"
        label="评论数"
        width="100"
      >
      </el-table-column>
      <el-table-column sortable prop="blog_date" label="发表时间" width="100">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="update(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="deleteUser(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <Pagination
      v-bind:child-msg="pageparm"
      @callFather="callFather"
    ></Pagination>
  </div>
</template>

<script>
import { blogall, select, del, sort } from "../../api/blogapi";
import { sortall } from "../../api/sortapi";
import { selects } from "../../api/blogsortapi";
import Pagination from "../../components/Pagination";
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: "添加",
      editForm: {
        blog_id: "",
        blog_sort: "",
        user_id: "",
        blog_title: "",
        blog_content: "",
        blog_views: "",
        blog_comment_count: "",
        blog_date: "",
        token: localStorage.getItem("logintoken"),
      },
      sortname: "",
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: "请输入描述名称", trigger: "blur" },
        ],
        deptNo: [{ required: true, message: "请输入描述id", trigger: "blur" }],
      },
      formInline: {
        page: 1,
        limit: 10,
        varLable: "",
        blog_id: "",
        varName: "",
        token: localStorage.getItem("logintoken"),
      },
      listDatas: [], //分类
      selectf: "",
      selectid: "",
      userparm: [], //搜索权限
      listData: [], //用户数据
      select: "",
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 15,
        total: 15,
      },
    };
  },
  components: {
    Pagination,
  },

  watch: {},

  created() {
    this.getdata(this.formInline);

    this.getdataf(this.formInline);
  },

  methods: {
    getdataf(parameter) {
      // 模拟数据
      let res = {
        code: 0,
        msg: null,
        count: 6,
      };
      this.loading = false;
      // 分页赋值
      this.pageparm.currentPage = this.formInline.page;
      this.pageparm.pageSize = this.formInline.limit;
      this.pageparm.total = res.count;
      sortall()
        .then((res) => {
          this.loading = false;
          if (res.success == 404) {
            this.$message({
              type: "info",
              message: res.msg,
            });
          } else {
            this.listDatas = res;
            this.pageparm.currentPage = this.formInline.page;
            this.pageparm.pageSize = this.formInline.limit;
            this.pageparm.total = res.count;
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message.error("获取分类列表失败，请稍后再试！");
        });
    },

    getdata(parameter) {
      // 模拟数据
      let res = {
        code: 0,
        msg: null,
        count: 6,
      };
      this.loading = false;
      this.pageparm.currentPage = this.formInline.page;
      this.pageparm.pageSize = this.formInline.limit;
      this.pageparm.total = res.count;
      blogall()
        .then((res) => {
          this.loading = false;
          if (res.success == 404) {
            this.$message({
              type: "info",
              message: res.msg,
            });
          } else {
            this.listData = res;
            for (var i = 0; i <= this.listData.length; i++) {
              this.listData[i].blog_date = this.GetDateFormat(
                this.listData[i].blog_date
              );
            }

            this.pageparm.currentPage = this.formInline.page;
            this.pageparm.pageSize = this.formInline.limit;
            this.pageparm.total = res.count;
          }
        })
        .catch((err) => {
          this.loading = false;
          this.$message({
            type: "success",
            message: "获取博文成功！",
          });
        });
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.getdata(this.formInline);
    },
    // 搜索事件
    search() {
      for (var i = 0; i < this.listDatas.length; i++) {
        if (this.listDatas[i].sort_name == this.selectf) {
          this.selectid = this.listDatas[i].sort_id;
        }
      }
      if (this.formInline.blog_id == "") {
        sort(this.selectid).then((res) => {
          this.listData.splice(res);
          this.listData.push(res);
          this.listData = JSON.parse(JSON.stringify(this.listData[0]));
          for (var i = 0; i <= res.length; i++) {
            this.listData[i].blog_date = this.GetDateFormat(
              this.listData[i].blog_date
            );
          }
        });
      } else {
        select(this.formInline.blog_id).then((res) => {
          if (res == 404) {
            this.$message({
              type: "error",
              message: "查询的id不存在",
            });
          } else {
            this.listData.splice(res);
            this.listData.push(res);
            blogall().then((res) => {
              this.listDatas = res;
            });
            this.pageparm.currentPage = this.formInline.page;
            this.pageparm.pageSize = this.formInline.limit;
            this.pageparm.total = res.count;
          }
        });
      }
    },
    GetDateFormat(str) {
      return new Date(parseInt(str.substr(6, 13))).toLocaleDateString();
    },

    update(index, row) {
      selects(row.blog_id).then((res) => {

        for (var i = 0; i <= this.listDatas.length; i++) {
          if (this.listDatas[i].sort_id == res.sort_id){
            this.editForm.blog_id = row.blog_id;
            this.editForm.blog_title = row.blog_title;
            this.editForm.blog_content = row.blog_content;
            this.editForm.blog_date = row.blog_date;
            this.editForm.user_id = row.user_id;
            this.editForm.blog_comment_count = row.blog_comment_count;
            this.editForm.blog_views = row.blog_views;
            this.editForm.blog_sort = this.listDatas[i].sort_name;
            this.$router.push({
              path: "/blog/blog",
              query: { shuju: this.editForm },
            });
          }
        }
      });
    },

    tianjia() {
      this.$router.push({
        path: "/blog/blog",
      });
    },

    // 删除角色
    deleteUser(index, row) {
      this.$confirm("确定要删除吗?", "信息", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          del(row.blog_id)
            .then((res) => {
              if (res == 200) {
                this.$message({
                  type: "success",
                  message: "博文已删除！",
                });
                this.getdata(this.formInline);
              } else {
                this.$message({
                  type: "info",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              this.loading = false;
              this.$message.error("博文删除失败，请稍后再试！");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 数据权限

    // 选中菜单
    changemenu(arr) {
      let change = [];
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].checked) {
          change.push(arr[i].id);
        }
      }
      this.checkmenu = change;
    },

    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    },
  },
};
</script>

<style>
</style>
 
 